import { Injectable } from '@angular/core';
import {BehaviorSubject} from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class SearchMaintainService {

    regionMngDataChange = new BehaviorSubject<RegionMngModel>({
        sectionId: '',
        stationId: '',
        adcd: ''
    });
    // mngDataChange = new BehaviorSubject(null);
  constructor() { }

    // 存储历史选择记录
    setLatestList(comp, selection): void {
        const list = sessionStorage.getItem(comp) ? JSON.parse(sessionStorage.getItem(comp)).list : [];
        // 如果选中的keyword存在于localStorage，则不存这条记录
        const preItem = list.find(res => {
            return res && res.keyword === selection;
        });
        if (preItem) {
            return;
        }
        const result = {
            keyword: selection
        };
        list.unshift(result);
        // 最多储存5条历史记录
        if (list.length > 5) { list.pop(); }
        sessionStorage.setItem(comp, JSON.stringify({list: list}));
    }

    // 获取历史选择记录
    getLatestList(comp): Array<any> {
        return sessionStorage.getItem(comp) ? JSON.parse(sessionStorage.getItem(comp)).list : [];
    }
}

export interface RegionMngModel {
  stationId: string; //  管理处编码
  sectionId: string; //  管理所编码
  adcd: string; //  行政区划编码
  [key: string]: any;
}
